package com.hex.ds.hdtp.core.app.script.test.service.impl;

import com.hex.ds.hdtp.core.app.common.enums.ScriptTableConsanguinityFrom;
import com.hex.ds.hdtp.core.app.common.page.PageData;
import com.hex.ds.hdtp.core.app.common.page.converter.IPageAndPageDtoConverter;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.script.test.converter.IScriptDataTestListConverter;
import com.hex.ds.hdtp.core.app.script.common.converter.IScriptTableConsanguinityConverter;
import com.hex.ds.hdtp.core.app.script.test.dto.request.ScriptDataTestListRequestDto;
import com.hex.ds.hdtp.core.app.script.test.dto.response.ScriptDataTestListResponseDto;
import com.hex.ds.hdtp.core.app.script.common.dto.response.ScriptTableConsanguinityResponseDto;
import com.hex.ds.hdtp.core.app.script.test.service.IScriptDataTestListService;
import com.hex.ds.hdtp.core.app.script.common.service.IScriptTableConsanguinityService;
import com.hex.ds.hdtp.core.inf.common.page.Page;
import com.hex.ds.hdtp.core.inf.script.test.po.ScriptDataTestListPo;
import com.hex.ds.hdtp.core.inf.script.test.repository.IScriptDataTestListRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Package: com.hex.ds.hdtp.core.app.script.test.service.impl
 * @ClassName ScriptDataTestListService
 * @Description: 带数据测试脚本清单-业务接口实现
 * @Author: wz.li
 * @Date 2023/8/1 16:21
 * @Version v2.0.2
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ScriptDataTestListService implements IScriptDataTestListService {

    private final IScriptDataTestListRepository scriptDataTestListRepository;

    private final IScriptDataTestListConverter converter;

    private final IPageAndPageDtoConverter pageAndPageDtoConverter;

    private final IScriptTableConsanguinityConverter scriptTableConsanguinityConverter;

    private final IScriptTableConsanguinityService scriptTableConsanguinityService;

    /*
     * @Method: queryListByPage <br>
     * @Param: [scriptDataTestListRequestDto, pageDto] <br>
     * @Return: com.hex.ds.hdtp.core.app.common.page.PageData <br>
     * @Description：分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/1 16:22 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public PageData queryListByPage(ScriptDataTestListRequestDto scriptDataTestListRequestDto, PageDto pageDto) {
        final Page<ScriptDataTestListPo> pageResult = scriptDataTestListRepository.queryListByPage(
                converter.toScriptDataTestListPo(scriptDataTestListRequestDto), pageAndPageDtoConverter.toPage(pageDto));
        List<ScriptDataTestListResponseDto> responseDtos = converter.toScriptDataTestListResponseDtoList(pageResult.getData());
        return new PageData(responseDtos, pageResult.getTotal());
    }

    /*
     * @Method: queryConsanguinityListByPage <br>
     * @Param: [scriptDataTestListRequestDto, pageDto] <br>
     * @Return: com.hex.ds.hdtp.core.app.common.page.PageData <br>
     * @Description：分页查询测试脚本的血缘<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/3 17:49 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public PageData queryConsanguinityListByPage(ScriptDataTestListRequestDto scriptDataTestListRequestDto, PageDto pageDto) {
        final Page<ScriptDataTestListPo> pageResult = scriptDataTestListRepository.queryConsanguinityListByPage(
                converter.toScriptDataTestListPo(scriptDataTestListRequestDto), pageAndPageDtoConverter.toPage(pageDto));
        List<ScriptDataTestListResponseDto> responseDtos = converter.toScriptDataTestListResponseDtoList(pageResult.getData());
        return new PageData(responseDtos, pageResult.getTotal());
    }

    /*
     * @Method: queryList <br>
     * @Param: [scriptDataTestListRequestDto] <br>
     * @Return: java.util.List<com.hex.ds.hdtp.core.app.script.test.dto.response.ScriptDataTestListResponseDto> <br>
     * @Description：不分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/1 16:22 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public List<ScriptDataTestListResponseDto> queryList(ScriptDataTestListRequestDto scriptDataTestListRequestDto) {
        final List<ScriptDataTestListPo> dataList = scriptDataTestListRepository.queryList(
                converter.toScriptDataTestListPo(scriptDataTestListRequestDto));
        return converter.toScriptDataTestListResponseDtoList(dataList);
    }

    /*
     * @Method: queryListByTaskId <br>
     * @Param: [pkId] <br>
     * @Return: java.util.List<com.hex.ds.hdtp.core.app.script.test.dto.response.ScriptDataTestListResponseDto> <br>
     * @Description：根据测试任务主键查询任务脚本清单<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/4 11:43 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public List<ScriptDataTestListResponseDto> queryListByTaskId(String taskPkId) {
        return queryList(new ScriptDataTestListRequestDto().setTaskPkId(taskPkId));
    }

    /**
     * @Method queryById <br>
     * @Param
     * @Param pkId <br>
     * @Return com.hex.ds.hdtp.core.app.script.test.dto.response.ScriptDataTestListResponseDto <br>
     * @Description 根据id查询<br>
     * @Author jian.qiao<br>
     * @Date 2023/10/27 18:37 <br>
     * @Version V1.0<br>
     */
    @Override
    public ScriptDataTestListResponseDto queryById(String pkId) {
        return converter.toScriptDataTestListResponseDto(scriptDataTestListRepository.queryById(pkId));
    }

    /*
     * @Method: add <br>
     * @Param: [scriptDataTestListRequestDto] <br>
     * @Return: void <br>
     * @Description：新增<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/1 16:22 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void add(ScriptDataTestListRequestDto scriptDataTestListRequestDto) {
        final ScriptDataTestListPo scriptDataTestListPo = converter.toScriptDataTestListPo(scriptDataTestListRequestDto);
        if (scriptDataTestListRepository.add(scriptDataTestListPo) != 1) {
            throw new RuntimeException("脚本测试清单-新增失败");
        }
        initScriptTableConsanguinity(scriptDataTestListPo);
    }

    /*
     * @Method: initScriptTableConsanguinity <br>
     * @Param: [scriptDataTestListPo] <br>
     * @Return: void <br>
     * @Description：初始化测试脚本的表血缘<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/3 12:00 <br>
     * @Version： V2.0.2<br>
     */
    private void initScriptTableConsanguinity(ScriptDataTestListPo scriptDataTestListPo) {
        final List<ScriptTableConsanguinityResponseDto> scriptTableConsanguinityResponseDtos =
                scriptTableConsanguinityService.queryList(scriptDataTestListPo.getScriptConvertListPkId(), ScriptTableConsanguinityFrom.SCRIPT_CONVERT_TARGET.getName());
        for (ScriptTableConsanguinityResponseDto scriptTableConsanguinityResponseDto : scriptTableConsanguinityResponseDtos) {
            scriptTableConsanguinityService.add(
                    scriptTableConsanguinityConverter.toScriptTableConsanguinityRequestDto(
                            scriptTableConsanguinityResponseDto, scriptDataTestListPo));
        }
    }

    /*
     * @Method: modifyById <br>
     * @Param: [scriptDataTestListRequestDto] <br>
     * @Return: void <br>
     * @Description：修改<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/1 16:22 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public void modifyById(ScriptDataTestListRequestDto scriptDataTestListRequestDto) {
        int count = scriptDataTestListRepository.modifyById(converter.toScriptDataTestListPo(scriptDataTestListRequestDto));
        if (count != 1) {
            throw new RuntimeException("脚本测试清单-更新失败");
        }
    }

    /*
     * @Method: removeById <br>
     * @Param: [pkId] <br>
     * @Return: void <br>
     * @Description：删除<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/1 16:22 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void removeById(String pkId) {
        scriptTableConsanguinityService.removeByScriptListPkId(pkId, ScriptTableConsanguinityFrom.SCRIPT_TEST.getName());
        int count = scriptDataTestListRepository.removeById(pkId);
        if (count != 1) {
            throw new RuntimeException("脚本测试清单-删除失败");
        }
    }
}
